package com.qf.service.impl;

import com.qf.dao.XcJqMapper;
import com.qf.pojo.po.XcJq;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.XcJqService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 景区
 * @author WYD
 * @date 2021/10/25  16:22
 */
@Service
public class XcJqServiceImpl implements XcJqService {

    @Autowired
    XcJqMapper xcJqMapper;

    @Autowired
    RedisTemplate redisTemplate;


    protected Logger logger = LoggerFactory.getLogger(XcJqServiceImpl.class);

    /**
     * 热度查询全部景区
     * @return
     */
    @Override
    public BaseResp findAreaHot() {
        List<XcJq> list = new ArrayList<>();
        Boolean xcjp_list = redisTemplate.hasKey("AREA_HOT_ALL");
        if (!xcjp_list){
            list = xcJqMapper.findAreaHot();
            logger.info("从数据库查询的结果集为:"+list);
            redisTemplate.opsForList().leftPushAll("AREA_HOT_ALL",list);
        }else {
            list = redisTemplate.opsForList().range("AREA_HOT_ALL", 0, -1);
            //设置存活时间
            redisTemplate.expire("AREA_HOT_ALL",60, TimeUnit.SECONDS);
            logger.info("从redis中查询的结果集为:"+list);
        }
        return new BaseResp(0,"查询成功",null,list);
    }

    /**
     *  用户输入城市，查询热门景区
     *    ++ 区景区的热度
     * @param
     * @return
     */
    @Override
    public BaseResp findByCityId(Integer cityId) {
        List<XcJq> list = null;
        Boolean xcjp_list = redisTemplate.hasKey("CITY_JQ_HOST"+cityId.toString());
        if (!xcjp_list){
            list = xcJqMapper.findByCityId(cityId);
            logger.info("从数据库查询的结果集为:"+list);
            redisTemplate.opsForList().leftPushAll("CITY_JQ_HOST"+cityId.toString(),list);
        }else {
            list = redisTemplate.opsForList().range("CITY_JQ_HOST"+cityId.toString(), 0, -1);
            logger.info("从redis中查询的结果集为:"+list);
        }
        return new BaseResp(0,"查询成功",null,list);
    }

    /**
     * 区里的景区
     * @param areaId
     * @return
     */
    @Override
    public BaseResp findByCityAreaId(Integer areaId) {
        List<XcJq> list = null;
        Boolean xcjp_list = redisTemplate.hasKey("AREA_JQ_ALL"+areaId.toString());
        if (!xcjp_list){
            list = xcJqMapper.findByCityAreaId(areaId);
            logger.info("从数据库查询的结果集为:"+list);
            redisTemplate.opsForList().leftPushAll("AREA_JQ_ALL"+areaId.toString(),list);
        }else {
            list = redisTemplate.opsForList().range("AREA_JQ_ALL"+areaId.toString(), 0, -1);
            logger.info("从redis中查询的结果集为:"+list);
        }
        return new BaseResp(0,"查询成功",null,list);
    }

    /**
     * 单个景区详情
     * @param qcId
     * @return
     */
    @Override
    public BaseResp findQcDetailByQcId(Integer qcId) {
        Object list1 = null;
        Object list = redisTemplate.opsForHash().get("JQ_DETAIL_ALL"+qcId.toString(),qcId.toString());
        if (list==null){
            list1 =  xcJqMapper.findQcDetailByQcId(qcId);
            logger.info("123从数据库查询的结果集为:"+list1);
            redisTemplate.opsForHash().put("JQ_DETAIL_ALL"+qcId.toString(),qcId.toString(),list1);
        }else {
            list1 = redisTemplate.opsForHash().get("JQ_DETAIL_ALL"+qcId.toString(), qcId.toString());
            logger.info("123从redis中查询的结果集为:"+list1);
        }
        return new BaseResp(0,"查询成功",null,list1);
    }

}
